package com.lsfb.signsys.view.BottomNavView;

import android.content.Context;
import android.graphics.Color;
import android.icu.text.UFormat;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lsfb.signsys.utils.DensityUtils;

/**
 * Created by zgscwjm on 2018/4/10.
 */

public class BageWrapper extends FrameLayout{


    private BadgeView mBadgeView;
    public BageWrapper(@NonNull Context context) {
        this(context,null);
    }

    public BageWrapper(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BageWrapper(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        mBadgeView = new BadgeView(context);


    }


//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
//        View view = getChildAt(0);
////        MeasureSpec.getMode(view.getMeasuredWidthAndState());
//        super.onMeasure(view.getMeasuredWidthAndState(), view.getMeasuredHeightAndState());
//    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (getChildCount()==1) {
            View mtargView =getChildAt(0);

            mBadgeView.setVisibility(View.VISIBLE);

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( Math.min(mtargView.getHeight(),50), Math.min(mtargView.getHeight(),50));
//            layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
//            layoutParams.gravity = Gravity.TOP|Gravity.RIGHT;
            layoutParams.gravity = Gravity.RIGHT;
            mBadgeView.setLayoutParams(layoutParams);
            addView(mBadgeView);
            postInvalidate();
        }
    }

    public static void addBage(Context context, View target) {


        BadgeView badgeView = new BadgeView(context);
        badgeView.setVisibility(View.VISIBLE);

        ViewParent parent = target.getParent();

        if (parent instanceof BageWrapper){
            return;
        }
        ViewGroup.LayoutParams lp = target.getLayoutParams();
        ViewGroup group = (ViewGroup) parent;

        BageWrapper container = new BageWrapper(context);
        int index = group.indexOfChild(target);


        group.removeView(target);


        group.addView(container, index, lp);
        container.addView(target, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));


        int height = target.getMeasuredHeight();

        container.addView(badgeView, new FrameLayout.LayoutParams(height, height, Gravity.TOP | Gravity.RIGHT));
        group.invalidate();
    }


}
